// 登录业务模块
console.log( '登录业务模块...' )

import axios from "axios"
import baseURL from './webpack-env'

// 获取登录成功之后目标跳转地址
let targetURL = localStorage.getItem('targetURL')

$('.form-signin').submit(function (e){
    // 阻止表单默认提交
    e.preventDefault()

    var user = $('#user').val()
    var pass = $('#pass').val()

    var reg = /^\w{6,16}$/
    if (!reg.test(user) || !reg.test(pass)) {
        alert('账号或密码格式错误')
        return
    }

    // 发送登录请求
    // axios.post('http://localhost:8080/user/login',{user,pass})
    axios.post(baseURL + '/user/login',{user,pass})
    .then((res)=>{
        let {data} = res// {data:{...}}
        // 反馈登录结果给用户
        alert(data.msg)
        if (data.code === 1) {
            // 存储已登录的token
            localStorage.setItem('token',data.token)

            // 跳转到首页
            // location.href = './index.html' 

            // 登录成功,跳转到原来的页面
            location.href = targetURL
        }
    })
})
